var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var Wx_config = require('../../wx_config/wx_config');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);


router.get('/jkyx/access_token', function(req, res, next) {
	pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		var sql = 'select * from access_token order by id desc limit 1';
		connection.query(sql,function (err, results) {
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var resultsLen = results.length;
				// 首次 收据库 没数据
				if (resultsLen == 0) {
					var urlStr = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + Wx_config.AppID + '&secret='  + Wx_config.Secret;
					request(urlStr, function (error, response, body) {
						if (!error && response.statusCode == 200) {
							var body = JSON.parse(body);
							var access_token = body.access_token;
							var create_time = CommonJS.timestamp();
							var insertSql = 'insert into access_token (access_token,create_time) values (?,?)';
							connection.query(insertSql,[access_token,create_time],function (err, results) {
								connection.release();
								if (err){
								  	res.end(JSON.stringify({
						     			msg: '数据库查询失败',
						     			status:'103',
						     			err: err
						     		}));
								}else{
									res.end(JSON.stringify({
						     			msg: '操作成功',
						     			status:'100',
						     			access_token: access_token
						     		}));
								}
							})
						}
					})
				} else{ // 数据库有数据
					var item = results[0];
					var access_token = item.access_token; // 上一次的access_token
					var create_time = item.create_time; // 上一次的获取时间
					var create_time2 = CommonJS.timestamp(); // 当前时间戳
					var spec_time = create_time2 - create_time;
					if (spec_time > 6900) {  // 时间戳差值  1小时55分。  余留5分钟给系统。
						// 再次获取时间戳
						var urlStr = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + Wx_config.AppID + '&secret='  + Wx_config.Secret;
						request(urlStr, function (error, response, body) {
							if (!error && response.statusCode == 200) {
								var body = JSON.parse(body);
								var access_tokenr = body.access_token;
								var create_timer = CommonJS.timestamp();
								var insertSql = 'insert into access_token (access_token,create_time) values (?,?)';
								connection.query(insertSql,[access_tokenr,create_timer],function (err, results) {
									connection.release();
									if (err){
									  	res.end(JSON.stringify({
							     			msg: '数据库查询失败',
							     			status:'103',
							     			err: err
							     		}));
									}else{
										res.end(JSON.stringify({
							     			msg: '操作成功',
							     			status:'100',
							     			access_token: access_tokenr
							     		}));
									}
								})
							}
						})
					} else{
						connection.release();
						// 取上一次的时间戳
						res.end(JSON.stringify({
			     			msg: '操作成功',
			     			status:'100',
			     			access_token: access_token
			     		}));
					}
					
				}
			}
		})
	})
});



module.exports = router;

